package com.example.smartcity.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.activity.AdviceContentActivity;
import com.example.smartcity.activity.VideoContentActivity;
import com.example.smartcity.bean.AdviceBean;
import com.example.smartcity.bean.VideoBean;
import com.example.smartcity.utils.APIConfig;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class JLearnFragment extends BaseFragment {
    private static final String TAG = JLearnFragment.class.getSimpleName();


    final String url = APIConfig.BASE_URL;

    String result;
    private ListView lv_learn;
    private VideoBean videoBean;

    @Override
    public View initView() {

        Log.i(TAG, "视频学习");
        View view = View.inflate(getActivity(), R.layout.jd_learn, null);

        getVideoData();
        lv_learn = view.findViewById(R.id.jd_lv);
        return view;
    }

    @Override
    public void initData() {
        super.initData();

    }


    private void getVideoData(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(APIConfig.BASE_URL + "/videos"+"/allvideos")
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    result = response.body().string();
                    Log.i("videolist",result);
                    Gson gson = new Gson();
                    videoBean =  gson.fromJson(result, VideoBean.class);

                    Log.i("video", videoBean.getVideoList().toString());
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            VideoAdapter videoListAdapter = new VideoAdapter();
                            lv_learn.setAdapter(videoListAdapter);
                            lv_learn.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(getActivity(), VideoContentActivity.class);//todo
                                    intent.putExtra("videoid",videoBean.getVideoList().get(i).getId());
                                    intent.putExtra("videourl",videoBean.getVideoList().get(i).getVideo_url());
                                    intent.putExtra("videoauthornick",videoBean.getVideoList().get(i).getVideo_author_nickname());
                                    intent.putExtra("videoname",videoBean.getVideoList().get(i).getVideo_name());
                                    intent.putExtra("videotime",videoBean.getVideoList().get(i).getVideo_time());
                                    getActivity().startActivity(intent);
                                }
                            });
                        }
                    });
                }
            }
        });
    }

    public class VideoAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return videoBean!=null ? videoBean.getVideoList().size() : 0;
        }
        @Override
        public Object getItem(int i) {
            return videoBean.getVideoList().get(i);
        }
        @Override
        public long getItemId(int i) {
            return i;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            JLearnFragment.VideoAdapter.ViewHolder holder;
            if (view==null) {
                view = View.inflate(getContext(), R.layout.video_item,null);
                holder = new ViewHolder();
                holder.video_author = view.findViewById(R.id.video_author);
                holder.videoname = view.findViewById(R.id.video_name);
                holder.videotime = view.findViewById(R.id.video_time);
                holder.video_img = view.findViewById(R.id.video_img);


                view.setTag(holder);
            } else {
                holder = (VideoAdapter.ViewHolder) view.getTag();
            }
            Log.i("hello",result.toString());
            holder.videoname.setText(cutContext6(videoBean.getVideoList().get(i).getVideo_name()));
            Log.i("video_name", videoBean.getVideoList().get(i).getVideo_name());
            holder.video_author.setText(videoBean.getVideoList().get(i).getVideo_author_nickname());
            Log.i("video_author", videoBean.getVideoList().get(i).getVideo_author_nickname());
            holder.videotime.setText(videoBean.getVideoList().get(i).getVideo_time());
            Log.i("video_time", videoBean.getVideoList().get(i).getVideo_time());
            String videourl = videoBean.getVideoList().get(i).getVideo_picture_url();
            //Log.i("video_img_url",videoBean.getVideoList().get(i).getVideo_picture_url());
            Glide.with(getActivity()).load(APIConfig.BASE_URL+videourl).override(800,400).into(holder.video_img);
            if(videoBean.getVideoList().get(i).getVideo_picture_url()==null){
                Glide.with(getActivity()).load(APIConfig.BASE_URL+"/images/nopicture.jpg").override(400,400).into(holder.video_img);
                Log.i("if_learn","null image url");
            }else{Glide.with(getActivity()).load(APIConfig.BASE_URL+videoBean.getVideoList().get(i).getVideo_picture_url()).override(400,400).into(holder.video_img);}
            return view;
        }
        class ViewHolder{
            TextView video_author;
            TextView videoname;
            TextView videotime;
            ImageView video_img;

        }
    }

    public static void setListViewHeightBasedOnChildren(ListView listView){
        JLearnFragment.VideoAdapter videoAdapter = (JLearnFragment.VideoAdapter) listView.getAdapter();
        if (videoAdapter==null){
            return;
        }
        int totalHeight=0;
        Log.i("videoadapter.getCount()",videoAdapter.getCount()+"");
        for (int i=0;videoAdapter.getCount()>i;i++){
            View listItem = videoAdapter.getView(i,null,listView);
            // 计算子项View的宽高
            listItem.measure(0,0);
            // 统计所有子项的总高度
            totalHeight+=listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+(listView.getDividerHeight()*(videoAdapter.getCount()-1));
        Log.i("params.height",params.height+"");
        listView.setLayoutParams(params);
    }
    private String cutContext6(String str) {
        if (str.length() > 6){
            String str1 = str.substring(0, 6) + "...";
            return str1;
        } else {
            return str;
        }
    }

}
